import sys
import os
import argparse
import pandas as pd

sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from scripts.model_training import load_model

# 解析命令行参数
def parse_args():
    parser = argparse.ArgumentParser(description='单独预测一个学生的岗位推荐')
    parser.add_argument('--skills', type=str, required=True, help='学生技能')
    parser.add_argument('--education', type=str, required=True, help='学生教育程度')
    parser.add_argument('--target', type=str, required=True, help='学生目标')
    parser.add_argument('--major', type=str, required=True, help='学生专业')
    parser.add_argument('--experience', type=str, required=True, help='学生工作经验')
    return parser.parse_args()

# 使用模型进行岗位推荐
def recommend_job(student_data):
    # 加载模型
    model = load_model()
    
    if model is None:
        print('模型未加载，请先训练模型')
        return None
    
    # 将学生数据转换为DataFrame
    student_df = pd.DataFrame([student_data])
    
    # 预测岗位
    predicted_job = model.predict(student_df)
    
    # 获取预测概率
    probabilities = model.predict_proba(student_df)
    classes = model.classes_
    top_3_indices = probabilities[0].argsort()[-3:][::-1]
    
    # 打印推荐结果
    print(f"学生信息: {student_data}")
    print("推荐岗位及匹配概率:")
    for idx, i in enumerate(top_3_indices):
        print(f"{idx+1}. {classes[i]} (匹配概率: {probabilities[0][i]*100:.1f}%)")
    
    return predicted_job

if __name__ == '__main__':
    args = parse_args()
    student_data = {
        'skills': args.skills,
        'education': args.education,
        'target': args.target,
        'major': args.major,
        'experience': args.experience
    }
    recommend_job(student_data)